home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / latex209 / contrib / textyl / src / io.c < prev    next >
C/C++ Source or Header  |  1993-11-07  |  6KB  |  348 lines

  1. #include    "defs.h"
  2. #include    "globals.h"
  3.  
  4. static struct DVIBuftype
  5. {
  6.     int32           TotByteLen, curstrindex;
  7.     int32           maxlen;
  8.     short        *Dstring;
  9. }               GDVIBuf;
  10.  
  11. /*
  12.  * NOTATION:: All procedures that put a dvi-command into the temporary buffer are prefixed with "cmd"... Functions that deal
  13.  * with reading .tfm files are prefixed with "T" or have "tfm" in their names. Functions that deal with reading DVI files are
  14.  * prefixed with a "D".
  15.  */
  16.  
  17. /*--------------------------------------------*/
  18. void 
  19. cmd1byte(cmd)
  20.     int32           cmd;
  21. {
  22.     char           *realloc PP((char *, unsigned));
  23.  
  24.     if (GDVIBuf.curstrindex >= GDVIBuf.maxlen)
  25.     {            /* buffer full */
  26.         GDVIBuf.maxlen *= 2;
  27.         GDVIBuf.Dstring = (short *) realloc((char *)GDVIBuf.Dstring, (unsigned)sizeof(*GDVIBuf.Dstring) * GDVIBuf.maxlen);
  28.     }
  29.     GDVIBuf.Dstring[GDVIBuf.curstrindex] = cmd;
  30.     /* insert command byte */
  31.     GDVIBuf.TotByteLen++;
  32.     GDVIBuf.curstrindex++;
  33. }
  34.  
  35.  
  36. /*---------------------------------------------------*/
  37. void 
  38. cmd2byte(cmd)
  39.     int32           cmd;
  40. {
  41.     cmd1byte((cmd >> 8) & 0xff);
  42.     cmd1byte(cmd & 0xff);
  43. }
  44.  
  45. /*---------------------------------------------------*/
  46. void 
  47. cmd4byte(cmd)
  48.     int32           cmd;
  49. {
  50.     cmd1byte((cmd >> 24) & 0xff);
  51.     cmd1byte((cmd >> 16) & 0xff);
  52.     cmd1byte((cmd >> 8) & 0xff);
  53.     cmd1byte(cmd & 0xff);
  54. }
  55.  
  56.  
  57. /*---------------------------------------------------*/
  58. /*
  59.  * ### may be system dependent as integers are assumed to be signed 32-bits
  60.  */
  61.  
  62. void 
  63. cmdSigned(i, numbytes)
  64.     int32           i, numbytes;
  65. {
  66.     int32           tmp;
  67.  
  68.     if (numbytes == 4)
  69.     {
  70.         cmd4byte(i);
  71.         return;
  72.     }
  73.     tmp = i;
  74.     if (numbytes == 3)
  75.     {
  76.         if (tmp < 0)
  77.             tmp += TWO24;
  78.         cmd1byte(tmp / TWO16);
  79.         tmp &= TWO16 - 1;
  80.         cmd1byte(tmp / 256);
  81.     }
  82.     if (numbytes == 2)
  83.     {
  84.         if (tmp < 0)
  85.             tmp += TWO16;
  86.         cmd1byte(tmp / 256);
  87.     }
  88.     if (numbytes == 1)
  89.     {
  90.         if (tmp < 0)
  91.             tmp += 256;
  92.     }
  93.     cmd1byte(tmp & 255);    /* for all */
  94.  
  95.     /* <= 3 bytes */
  96. }
  97.  
  98. /*---------------------------------------------------*/
  99. int 
  100. Tgetc()
  101. {
  102.     int             c;
  103.  
  104.     if ((c = getc(tfmfile)) == EOF)
  105.     {
  106.         complain(ERRREALBAD);
  107.         fprintf(logfile, " early EOF of tfm file! \n");
  108.         return (0);
  109.     }
  110.     return (c & 0xff);
  111. }
  112.  
  113.  
  114. /*---------------------------------------------------*/
  115. void 
  116. readtfmword()
  117. {
  118.     b0 = Tgetc();
  119.     b1 = Tgetc();
  120.     b2 = Tgetc();
  121.     b3 = Tgetc();
  122. }
  123.  
  124.  
  125. /*---------------------------------------------------*/
  126. int32 
  127. Dgetc()
  128. {
  129.     int             c;
  130.  
  131.     if ((c = getc(dvifile)) == EOF)
  132.         return (0);
  133.     return (c & 0xff);
  134. }
  135.  
  136. /*---------------------------------------------------*/
  137. int32 
  138. Dget1byte()
  139. {
  140.     int             c;
  141.  
  142.     if ((c = getc(dvifile)) == EOF)
  143.         return (0);
  144.     else
  145.     {
  146.         cmd1byte(c);
  147.         return (c & 0xff);;
  148.     }
  149. }
  150.  
  151.  
  152. /*---------------------------------------------------*/
  153. int32 
  154. Dsign1byte()
  155. {
  156.     int32           c;
  157.  
  158.     c = Dgetc();
  159.     if (c >= 128)
  160.         c -= 256;
  161.     cmd1byte(c);
  162.     return (c);
  163. }
  164.  
  165.  
  166. /*---------------------------------------------------*/
  167. int32 
  168. Dget2byte()
  169. {
  170.     int32           a, b;
  171.  
  172.     a = Dgetc();
  173.     b = Dgetc();
  174.     cmd1byte(a);
  175.     cmd1byte(b);
  176.     return ((a << 8) | b);
  177. }
  178.  
  179.  
  180. /*---------------------------------------------------*/
  181. int32 
  182. Dsign2byte()
  183. {
  184.     int32           a, b;
  185.  
  186.     a = Dgetc();
  187.     b = Dgetc();
  188.     cmd1byte(a);
  189.     cmd1byte(b);
  190.     if (a < 128)
  191.         return ((a >> 8) | b);
  192.     else
  193.         return ((a - 256) * 256 + b);
  194. }
  195.  
  196.  
  197. /*---------------------------------------------------*/
  198. int32 
  199. Dget3byte()
  200. {
  201.     int32           a, b, c;
  202.  
  203.     a = Dgetc();
  204.     b = Dgetc();
  205.     c = Dgetc();
  206.     cmd1byte(a);
  207.     cmd1byte(b);
  208.     cmd1byte(c);
  209.     return ((a << 16) | (b << 8) | c);
  210. }
  211.  
  212.  
  213. /*---------------------------------------------------*/
  214. int32 
  215. Dsign3byte()
  216. {
  217.     int32           a, b, c;
  218.  
  219.     a = Dgetc();
  220.     b = Dgetc();
  221.     c = Dgetc();
  222.     cmd1byte(a);
  223.     cmd1byte(b);
  224.     cmd1byte(c);
  225.     if (a < 128)
  226.         return ((a << 16) | (b << 8) | c);
  227.     else
  228.         return ((a - 256) * 256 + b) * 256 + c;
  229. }
  230.  
  231.  
  232. /*---------------------------------------------------*/
  233. int32 
  234. Dsign4byte()
  235. {
  236.     int32           a, b, c, d;
  237.  
  238.     a = Dgetc();
  239.     b = Dgetc();
  240.     c = Dgetc();
  241.     d = Dgetc();
  242.     cmd1byte(a);
  243.     cmd1byte(b);
  244.     cmd1byte(c);
  245.     cmd1byte(d);
  246.     if (a < 128)
  247.         return (((a * 256 + b) * 256 + c) * 256 + d);
  248.     else
  249.         return ((((a - 256) * 256 + b) * 256 + c) * 256 + d);
  250. }
  251.  
  252.  
  253. /*---------------------------------------------------*/
  254. /*
  255.  * write a byte out to the ouput file, but if we encounter the font flag, define the new fonts, and continue
  256.  */
  257. void 
  258. OutputByte(b)
  259.     int32           b;
  260. {
  261.     if (b == OURFONTFLAG)
  262.     {            /* our special macro-flag */
  263.         b = NOP;    /* nullify it */
  264.         if (!didnewfonts)
  265.         {
  266.             didnewfonts = true;
  267.             defineNewfonts();    /* expand the defns in the outfile itself */
  268.         }
  269.     }            /* if */
  270.     putc(b, outputfil);
  271.     TotBytesWritten++;    /* keep count of all bytes */
  272. }
  273.  
  274.  
  275. /*---------------------------------------------------*/
  276. void 
  277. Output2Byte(i)
  278.     int32           i;
  279. {
  280.     OutputByte((i >> 8) & 0xff);
  281.     OutputByte(i & 0xff);
  282. }
  283.  
  284.  
  285. /*---------------------------------------------------*/
  286. void 
  287. Output4Byte(i)
  288.     int32           i;
  289. {
  290.     OutputByte((i >> 24) & 0xff);
  291.     OutputByte((i >> 16) & 0xff);
  292.     OutputByte((i >> 8) & 0xff);
  293.     OutputByte(i & 0xff);
  294. }
  295.  
  296. /*---------------------------------------------------*/
  297. void 
  298. InitDVIBuf()
  299. {
  300.     GDVIBuf.maxlen = 2048;
  301.     GDVIBuf.Dstring = (short *) xmalloc(sizeof(*GDVIBuf.Dstring) * 2048);
  302.     GDVIBuf.TotByteLen = 0;
  303.     GDVIBuf.curstrindex = 0;
  304. }
  305.  
  306.  
  307. /*---------------------------------------------------*/
  308. void 
  309. ClearDVIBuf()
  310. {
  311.     GDVIBuf.TotByteLen = 0;
  312.     GDVIBuf.curstrindex = 0;
  313. }
  314.  
  315.  
  316. /*---------------------------------------------------*/
  317. void 
  318. WriteDVIBuf()
  319. {
  320.     int32           i;
  321.     int32           b;
  322.  
  323.     for (i = 0; i < GDVIBuf.curstrindex; i++)
  324.     {
  325.         b = GDVIBuf.Dstring[i];
  326.         OutputByte(b);
  327.     }
  328.     ClearDVIBuf();
  329. }
  330.  
  331.  
  332. /*---------------------------------------------------*/
  333. void 
  334. BackupInBuf(nbytes)
  335.     int             nbytes;
  336. {
  337.     GDVIBuf.curstrindex -= nbytes;    /* points to position to-be-filled */
  338.     GDVIBuf.TotByteLen -= nbytes;
  339. }
  340.  
  341.  
  342. /*-----------------------------------------------------*/
  343. int32 
  344. DVIMark()
  345. {
  346.     return (GDVIBuf.TotByteLen);
  347. }
  348.